package com.np.dialogdemo1;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // AlertDialog - 简单对话框
        findViewById(R.id.button_01).setOnClickListener(v -> {
            AlertDialog alertDialog = new AlertDialog.Builder(this)
                    .setIcon(R.drawable.ic_launcher_background)
                    .setTitle("标题")
                    .setMessage("信息")
                    .setNegativeButton("阴性按钮", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //执行代码
                            dialogInterface.dismiss();//销毁对话框
                        }
                    })
                    .setPositiveButton("阳性按钮", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //执行代码
                            dialogInterface.dismiss();//销毁对话框
                        }
                    })
                    .create();

            alertDialog.show();
        });

        // AlertDialog - 列表对话框
        findViewById(R.id.button_02).setOnClickListener(v -> {
            String items[] = new String[]{"item1", "item2", "...."};

            AlertDialog alertDialog = new AlertDialog.Builder(this)
                    .setIcon(R.drawable.ic_launcher_background)
                    .setTitle("标题")
                    .setItems(items, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //i表示点击的指针，可使用 items[i]
                        }
                    })
                    .setNegativeButton("阴性按钮", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //执行代码
                            dialogInterface.dismiss();//销毁对话框
                        }
                    })
                    .setPositiveButton("阳性按钮", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //执行代码
                            dialogInterface.dismiss();//销毁对话框
                        }
                    })
                    .create();

            alertDialog.show();
        });

        // AlertDialog - 单选列表对话框
        findViewById(R.id.button_03).setOnClickListener(v -> {
            String items[] = new String[]{"item1", "item2", "...", "item2", "item2", "item2", "item2", "item2", "item2", "item2", "item2", "item2", "item2", "item2"};

            AlertDialog alertDialog = new AlertDialog.Builder(this)
                    .setIcon(R.drawable.ic_launcher_background)
                    .setTitle("标题")
                    .setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //   i 表示点击的指针，可使用 items[i]
                        }
                    })
                    .setNegativeButton("阴性按钮", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //执行代码
                            dialogInterface.dismiss();//销毁对话框
                        }
                    })
                    .setPositiveButton("阳性按钮", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //执行代码
                            dialogInterface.dismiss();//销毁对话框
                        }
                    })
                    .create();

            alertDialog.show();
        });

        // AlertDialog - 多选列表对话框
        findViewById(R.id.button_04).setOnClickListener(v -> {
            String items[] = new String[]{"item1", "item2", "..."};

            AlertDialog alertDialog = new AlertDialog.Builder(this)
                    .setIcon(R.drawable.ic_launcher_background)
                    .setTitle("标题")
                    .setMultiChoiceItems(items, new boolean[]{false, false, false},
                            new DialogInterface.OnMultiChoiceClickListener() {
                                public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                                    //   i 表示点击的指针，可使用 items[i]
                                }
                            })
                    .setNegativeButton("阴性按钮", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //执行代码
                            dialogInterface.dismiss();//销毁对话框
                        }
                    })
                    .setPositiveButton("阳性按钮", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //执行代码
                            dialogInterface.dismiss();//销毁对话框
                        }
                    })
                    .create();

            alertDialog.show();
        });

        // AlertDialog - 半自定义对话框
        findViewById(R.id.button_05).setOnClickListener(v -> {//半自定义对话框
            View view = LayoutInflater.from(this).inflate(R.layout.adapter_body_part, null);

            AlertDialog alertDialog = new AlertDialog.Builder(this)
                    .setIcon(R.drawable.ic_launcher_background)
                    .setTitle("标题")
                    .setView(view)
                    .setNegativeButton("阴性按钮", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //执行代码
                            dialogInterface.dismiss();//销毁对话框
                        }
                    })
                    .setPositiveButton("阳性按钮", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //执行代码
                            dialogInterface.dismiss();//销毁对话框
                        }
                    })
                    .create();
            alertDialog.show();
        });

        // AlertDialog - 全自定义对话框（不常用）
        findViewById(R.id.button_06).setOnClickListener(v -> {

        });

        // ProgressDialog - 圆形进度条对话框
        findViewById(R.id.button_07).setOnClickListener(v -> {
            ProgressDialog progressDialog = new ProgressDialog(this);
            progressDialog.setMessage("信息");
            progressDialog.show();
        });

        // ProgressDialog - 条形进度条对话框
        findViewById(R.id.button_08).setOnClickListener(v -> {
            ProgressDialog progressDialog = new ProgressDialog(this);
            progressDialog.setMessage("信息");
            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); // 水平方式
            progressDialog.setMax(100); // 设置最大值
            progressDialog.setProgress(10); // 设置当前进度
            progressDialog.show();
        });

        // DatePickerDialog - 日期选择弹出框
        findViewById(R.id.button_09).setOnClickListener(v -> {
            DatePickerDialog datePickerDialog = new DatePickerDialog(this);
            datePickerDialog.setOnDateSetListener(new DatePickerDialog.OnDateSetListener() {
                public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
                    //i 年 i1 月 i2 日
                    //关闭窗体
                    datePickerDialog.cancel();
                }
            });
            datePickerDialog.show();
        });

        // TimePickerDialog - 时间选择弹出框
        findViewById(R.id.button_10).setOnClickListener(v -> {
            TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
                public void onTimeSet(TimePicker timePicker, int i, int i1) {

                }
            }, 0, 0, true);
            timePickerDialog.show();
        });

        findViewById(R.id.button_single).setOnClickListener(v -> {
            showSingleChoiceDialog();
        });

        findViewById(R.id.button_multiple).setOnClickListener(v -> {
            showMultipleChoiceDialog();
        });

        findViewById(R.id.button_adapter).setOnClickListener(v -> {
            showAdapterDialog();
        });
    }

    private void showSingleChoiceDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final String[] genders = {"male", "female"};

        builder.setSingleChoiceItems(genders, -1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "你选择了 " + genders[which], Toast.LENGTH_LONG).show();
                dialog.cancel();
            }
        });

        builder.show();
    }

    private void showMultipleChoiceDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        String[] hobbies = {"乒乓球", "足球", "排球", "篮球"};
        boolean[] isChecked = {false, false, false, false};

        builder.setMultiChoiceItems(hobbies, isChecked, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                Toast.makeText(MainActivity.this, hobbies[which] + ":" + isChecked, Toast.LENGTH_LONG).show();
            }
        });

        builder.show();
    }

    private void showAdapterDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        String[] array = {"腹部", "心脏", "四肢"};
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.adapter_body_part, array);

        builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, array[which] + "被选择了", Toast.LENGTH_LONG).show();
            }
        });

        builder.show();
    }
}